from public.settins import Device,Response,Start_Thread,TTP_Parser
from apps.pssh import ssh,send_command,api,Port_command,Port_templates



def run(device: dict):
    try:
        d = Device(**device)
        c = ssh(d,application="更新接口")
        out = send_command(d,c,Port_command,application="更新接口",time_out=15)
        port= TTP_Parser(out,Port_templates)
        if port:
            r = Response(hostname=d.hostname,host=d.host,application="更新接口",data=port).Success
            api.post("/devops/device/port/",r)
            # print(r)
        else:
            r = Response(hostname=d.hostname, host=d.host, application="更新接口", data="接口获取异常！").Error
            api.error(r)
            # print(r)
    except Exception as e:
        r = Response(hostname=d.hostname, host=d.host, application="更新接口", data=str(e)).Error
        api.error(r)

def main():
    device_list = api.get_device
    Start_Thread(run, device_list)

if __name__ == '__main__':
    main()